const router = require("koa-router")()

router.prefix("/admin")

let { Info, InfoClass } = require("../models/info")

let { CtxBody, ForMatTime } = require("../models/config")

// 添加或修改信息分类

router.post("/addInfoClass", async ctx => {
    let { infoid, name, status } = ctx.request.body 
    if (infoid) {
        let res = await InfoClass.findOneAndUpdate({ _id: infoid }, {
            $set: {
                name,
                status
            }
        })
        CtxBody(ctx, 204, "")
    } else {
        let info = await InfoClass.findOne({ name })
        if (info) {
            CtxBody(ctx, 402, "")
        } else {
            let newClass = new InfoClass({
                name,
                status
            })
            let ret = await newClass.save()
            CtxBody(ctx, 202, "")
        }
        
    }
})

// 信息分类 显示和隐藏

router.post("/InfoChangeStatus", async ctx => {
    let { id } = ctx.request.body 
    if (id) {
        let info = await InfoClass.findOne({ _id: id })
        if (info. status == 1) {
            let up = await InfoClass.findOneAndUpdate({ _id: id }, {
                $set: {
                    status: "2"
                }
            })
            CtxBody(ctx, 204, "")
        } else {
            let up = await InfoClass.findOneAndUpdate({ _id: id }, {
                $set: {
                    status: "1"
                }
            })
            CtxBody(ctx, 204, "")
        }
    } else {
        CtxBody(ctx, 30003, "")
    }
})

// 获取所有信息分类

router.post("/getAllInfoClass", async ctx => {
    let { page } = ctx.request.body 
    console.log(page);
    
    let skipPage = page ? page : 1
    let PageSize = 10
    let res = await InfoClass.find().skip( (skipPage - 1) * PageSize).limit( PageSize )
    // console.log(res);
    
    if (res.length) {
        let info = []
        res.forEach(el => {
            let time = new Date(el.addtime).toLocaleString()
            info.push({
                addtime: time,
                name:el.name,
                status:el.status,
                _id: el._id
            })
        })
        let count = await InfoClass.count()
        
        
        if (info) {
            CtxBody(ctx, 200, {info, count})
        } else {
            CtxBody(ctx, 404, "")
        }
    } else {
        CtxBody(ctx, 404, "")
    }
    
})

// 获取所有新分类  -- 选择使用

router.post("/getInfoClassSelect", async ctx => {
    let res = await InfoClass.find({}, "_id name")
    if (res) {
        CtxBody(ctx, 200, res)
    } else {
        CtxBody(ctx, 404, "")
    }
})

// 获取所有的信息

router.post("/getAllInfoList", async ctx => {
    let { page } = ctx.request.body 
    let skipPage = page ? page : 1
    let PageSize = 10
    let res = await Info.find().populate(" itemClass").populate("sendBy").skip( (skipPage - 1) * PageSize).limit( PageSize )
    // console.log(res);
    
    if (res.length) {
        let info = []
        res.forEach(el => {
            let time = new Date(el.addtime).toLocaleString()
            info.push({
                addtime: time,
                title:el.title,
                status:el.status,
                _id: el._id,
                content: el.content,
                audio: el.audio,
                video: el.video,
                sendBy: el.sendBy,
                itemClass: el.itemClass
            })
        })
        let count = await Info.count()
        
        
        if (info) {
            CtxBody(ctx, 200, {info, count})
        } else {
            CtxBody(ctx, 404, "")
        }
    } else {
        CtxBody(ctx, 404, "")
    }
})


// 信息 显示和隐藏

router.post("/InfoListChangeStatus", async ctx => {
    let { id } = ctx.request.body 
    if (id) {
        let info = await Info.findOne({ _id: id })
        if (info. status == 1) {
            let up = await Info.findOneAndUpdate({ _id: id }, {
                $set: {
                    status: "2"
                }
            })
            CtxBody(ctx, 204, "")
        } else {
            let up = await Info.findOneAndUpdate({ _id: id }, {
                $set: {
                    status: "1"
                }
            })
            CtxBody(ctx, 204, "")
        }
    } else {
        CtxBody(ctx, 30003, "")
    }
})

// 添加信息

router.post("/addInfoList", async ctx => {
    let { title, content, _id, audio,video, status, itemClass } = ctx.request.body 
    let userid = ctx.state.user.id 
    if (_id) {
        let res = await Info.findOneAndUpdate({ _id }, {
            $set: {
                title,
                content,
                status,
                itemClass,
                audio,
                video,
                sendBy: userid
            }
        }, { new: true })
        if (res) {
            CtxBody(ctx, 204, "")
        } else {
            CtxBody(ctx, 404, "")
        }
        
    } else {
        let newInfo = new Info({
            title,
            content,
            audio,
            video,
            status,
            itemClass,
            sendBy: userid
        })
        let update = await newInfo.save()
        let updateClass = await InfoClass.findOneAndUpdate({ _id: itemClass }, {
            $push: {
                InfoList: update._id
            }
        })
        if (update) {
            CtxBody(ctx, 204, "")
        } else {
            CtxBody(ctx, 404, "")
        }
    }
})

// 根据ID 查找信息

router.post("/getInfoByID", async ctx => {
    let { id } = ctx.request.body 
    if (id) {
        let res = await Info.findOne({ _id: id })
        if (res) {
            CtxBody(ctx, 200, res)
        } else {
            CtxBody(ctx, 404, "")
        }
    } else {
        CtxBody(ctx, 30003, "")
    }
})




module.exports = router